<template>
	<a-card :bordered="false">
		<a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
			<a-row :gutter="24">
				<a-col :span="4">
					<a-form-item label="鸡群类别" name="category">
						<a-select v-model:value="searchFormState.category" placeholder="请选择鸡群类别" :options="categoryOptions"
								  @change="handleCategoryChange" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-form-item label="鸡群种类" name="type">
						<a-select v-model:value="searchFormState.type" placeholder="请选择鸡群种类"
								  :disabled="searchFormState.category === undefined" :options="typeOptions" allow-clear/>
					</a-form-item>
				</a-col>
				<a-col :span="5">
					<a-form-item label="所属栏区" name="columnId">
<!--						<a-input v-model:value="searchFormState.columnId" placeholder="请输入所属栏区" />-->
						<columnTreeSelect ref="columnTreeSelectRef" @update:columnId="handleColumnIdUpdate"/>
					</a-form-item>
				</a-col>
				<a-col :span="6">
					<a-form-item label="入栏日期" name="arrivalDate">
						<a-range-picker v-model:value="searchFormState.arrivalDate" show-time />
					</a-form-item>
				</a-col>
				<a-col :span="4">
					<a-button type="primary" @click="table.refresh(true)">查询</a-button>
					<a-button style="margin: 0 8px" @click="reset">重置</a-button>
				</a-col>
			</a-row>
		</a-form>
		<s-table
			ref="table"
			:columns="columns"
			:data="loadData"
			:alert="options.alert.show"
			bordered
			:row-key="(record) => record.id"
			:tool-config="toolConfig"
			:row-selection="options.rowSelection"
		>
			<template #operator class="table-operator">
				<a-space>
					<a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizChickenFlockAdd')">
						<template #icon><plus-outlined /></template>
						新增
					</a-button>
					<xn-batch-delete
						v-if="hasPerm('bizChickenFlockBatchDelete')"
						:selectedRowKeys="selectedRowKeys"
						@batchDelete="deleteBatchBizChickenFlock"
					/>
				</a-space>
			</template>
			<template #bodyCell="{ column, record }">
				<template v-if="column.dataIndex === 'category'">
					{{ $TOOL.dictTypeData('column_sign', record.category) }}
				</template>
				<template v-if="column.dataIndex === 'action'">
					<a-space>
						<a @click="formRef.onOpen(record)" v-if="hasPerm('bizChickenFlockEdit')">编辑</a>
						<a-divider type="vertical" v-if="hasPerm(['bizChickenFlockEdit', 'bizChickenFlockDelete'], 'and')" />
						<a-popconfirm title="确定要删除吗？" @confirm="deleteBizChickenFlock(record)">
							<a-button type="link" danger size="small" v-if="hasPerm('bizChickenFlockDelete')">删除</a-button>
						</a-popconfirm>
					</a-space>
				</template>
			</template>
		</s-table>
	</a-card>
	<Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="chickenflock">
	import tool from '@/utils/tool'
	import Form from './form.vue'
	import bizChickenFlockApi from '@/api/chickenflock/bizChickenFlockApi'
	let searchFormState = reactive({})
	const searchFormRef = ref()
	const table = ref()
	const formRef = ref()
	const columnTreeSelectRef = ref()
	const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
	// 查询区域显示更多控制
	const advanced = ref(false)
	const toggleAdvanced = () => {
		advanced.value = !advanced.value
	}
	const columns = [
		{
			title: '鸡群类别',
			dataIndex: 'category'
		},
		{
			title: '鸡群种类',
			dataIndex: 'type'
		},
		{
			title: '所属栏区',
			dataIndex: 'columnName'
		},
		{
			title: '鸡群数量',
			dataIndex: 'quantity'
		},
		{
			title: '入栏日期',
			dataIndex: 'arrivalDate'
		},
	]
	// 操作栏通过权限判断是否显示
	if (hasPerm(['bizChickenFlockEdit', 'bizChickenFlockDelete'])) {
		columns.push({
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		})
	}
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		// columns数字类型字段加入 needTotal: true 可以勾选自动算账
		alert: {
			show: true,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	const loadData = (parameter) => {
		const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
		// arrivalDate范围查询条件重载
		if (searchFormParam.arrivalDate) {
			searchFormParam.startArrivalDate = searchFormParam.arrivalDate[0]
			searchFormParam.endArrivalDate = searchFormParam.arrivalDate[1]
			delete searchFormParam.arrivalDate
		}
		return bizChickenFlockApi.bizChickenFlockPage(Object.assign(parameter, searchFormParam)).then((data) => {
			return data
		})
	}
	// 重置
	const reset = () => {
		Object.keys(searchFormState).forEach(key => {
			searchFormState[key] = undefined;
		});
		columnTreeSelectRef.value.clearColumnId();
		searchFormRef.value.resetFields()
		table.value.refresh(true)
	}
	// 删除
	const deleteBizChickenFlock = (record) => {
		let params = [
			{
				id: record.id
			}
		]
		bizChickenFlockApi.bizChickenFlockDelete(params).then(() => {
			table.value.refresh(true)
		})
	}
	// 批量删除
	const deleteBatchBizChickenFlock = (params) => {
		bizChickenFlockApi.bizChickenFlockDelete(params).then(() => {
			table.value.clearRefreshSelected()
		})
	}
	const categoryOptions = tool.dictList('column_sign')
	const typeOptions = ref([])
	// 根据鸡群类别来选择具体的种类
	const handleCategoryChange = value =>{
		// 先清空种类栏的值，以免出现类别与种类不匹配
		clearChickenType();
		typeOptions.value = value === '肉鸡' ? tool.dictList('broiler_type') : tool.dictList('layer_type');
		if (value === undefined){
			clearChickenType();
		}
	}

	const handleColumnIdUpdate = (value) =>{
		searchFormState.columnId = value;
	}

	const clearChickenType = () =>{
		searchFormState.type = undefined;
		typeOptions.value = [];
	}
</script>
